package be.xios.projectquiz.cards;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.JLabel;
import javax.swing.JPanel;

/// <summary>
/// Deze klasse is een cardpanel die een loadinganimatie bevat en een bijpassende tekst
/// vraagt om deze te weergeven
/// </summary>
/// <author>Sebastian Kubski</author>
///	<author>Robin Crombe: Gradient als achtergrond toegevoegd</author>
@SuppressWarnings("serial")
public class Loading extends JPanel {

	private JPanel mainPanel, messagePanel;
	private LoadingAnimation loadingPanel;
	
	/// <summary>
	/// Deze constructor 
	/// vraagt om deze te weergeven
	/// </summary>
	/// <author>Sebastian Kubski</author>
	public Loading(String message) {
		this.setLayout(new BorderLayout());
		
		mainPanel = new JPanel(new BorderLayout()){
            protected void paintComponent(Graphics grphcs) {
                Graphics2D g2d = (Graphics2D) grphcs;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);

                GradientPaint gp = new GradientPaint(0, 0, getBackground().brighter().brighter(), getWidth(), getHeight(), getBackground().darker().darker());

                g2d.setPaint(gp);
                g2d.fillRect(0, 0, getWidth(), getHeight());

				super.paintComponent(grphcs);
            }
        };
        mainPanel.setOpaque(false);
		
			loadingPanel = new LoadingAnimation();
			loadingPanel.setOpaque(false);

			messagePanel = new JPanel();
				messagePanel.setBackground(Color.BLACK);
			
				JLabel messageLbl = new JLabel(message, JLabel.HORIZONTAL);
				messageLbl.setOpaque(true);
				messageLbl.setForeground(Color.WHITE);
				messageLbl.setBackground(Color.BLACK);
				messageLbl.setFont(new Font("Euphemia", Font.BOLD, 15));
				
				messagePanel.add(messageLbl);
				
			mainPanel.add(loadingPanel, BorderLayout.CENTER);
			mainPanel.add(messagePanel, BorderLayout.SOUTH);
		
		add(mainPanel, BorderLayout.CENTER);
	
		playAnimation(loadingPanel);
	} 
	
	/// <summary>
	/// Deze methods zorgt dat de runnable executed wordt
	/// </summary>
	/// <parameters>een loadingpanel: deze bevat de runnable method</parameters>
	/// <author>Sebastian Kubski</author>
	private void playAnimation(LoadingAnimation loadingPanel) {
		 ExecutorService executor = Executors.newCachedThreadPool();
         executor.execute(loadingPanel);     
	}
}
